package deepbrain.simnetwork.loader.vertex;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import deepbrain.simnetwork.exception.SimNetworkException;
import deepbrain.simnetwork.loader.ConnectionsLoader;
import deepbrain.simnetwork.mechanism.Mechanism;
import deepbrain.simnetwork.structure.Connection;

public class VertexConnectionsLoader implements ConnectionsLoader {

	protected VertexesLoader loader;

	public VertexesLoader getVertexesLoader() {
		return loader;
	}

	public void setVertexesLoader(VertexesLoader loader) {
		this.loader = loader;
	}

	public Set<Connection<?>> loadConnections(int numOfNodes)
			throws SimNetworkException {
		List<VertexInformation> vertexes = loader.loadVertexes();
		Set<Connection<?>> connections = new HashSet<Connection<?>>();
		for (VertexInformation vertex : vertexes) {
			for (Integer afferentNode : vertex.getAfferentNodes()) {
				Connection<?> connection = new Connection<Boolean>(
						afferentNode, vertex.getNodeNo());
				connections.add(connection);
			}
		}
		return connections;
	}

	public Mechanism[] loadMechanisms(int numOfNodes)
			throws SimNetworkException {
		List<VertexInformation> vertexes = loader.loadVertexes();
		Mechanism[] mechanisms = new Mechanism[numOfNodes];
		for (VertexInformation vertex : vertexes) {
			Mechanism mechanism = vertex.getMechanism();
			if (mechanism!=null)
				mechanism.initialize(vertex.getMechanismProperties());
			mechanisms[vertex.getNodeNo()] = mechanism;
		}
		return mechanisms;
	}
}
